package cn.hdy.org.controller;

import cn.hdy.base.util.AjaxResult;
import cn.hdy.base.util.PageList;
import cn.hdy.org.domain.Shop;
import cn.hdy.org.query.ShopQuery;
import cn.hdy.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺管理的CRUD", description = "店铺管理的CRUD")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @GetMapping
    @ApiOperation(value = "查询所有店铺")
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个店铺")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findOne(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个店铺")
    public AjaxResult delete(@PathVariable("id") Long id) {
        shopService.del(id);
        return AjaxResult.me();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除店铺")
    public AjaxResult delPatch(@RequestBody Long[] ids) {
        shopService.patchDel(ids);
        return AjaxResult.me();
    }

    @PutMapping
    @ApiOperation(value = "添加或修改店铺")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {

        if (shop.getId() == null) {
            shopService.add(shop);
        } else {
            shopService.update(shop);
            System.out.println(shop);
        }
            return AjaxResult.me();
    }

    @PostMapping
    @ApiOperation(value = "店铺的分页查询")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery) {
        return shopService.queryPage(shopQuery);
    }
}
